﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace SCG.TurboSprite.TileBased
{
    //A line segment, used when determining adjacency
    public struct Segment
    {
        //Coordinates
        public Segment(int x1, int y1, int x2, int y2)
        {
            _x1 = x1;
            _y1 = y1;
            _x2 = x2;
            _y2 = y2;
        }

        public int X1
        {
            get
            {
                return _x1;
            }
            set
            {
                _x1 = value;
            }
        }
        public int Y1
        {
            get
            {
                return _y1;
            }
            set
            {
                _y1 = value;
            }
        }
        public int X2
        {
            get
            {
                return _x2;
            }
            set
            {
                _x2 = value;
            }
        }
        public int Y2
        {
            get
            {
                return _y2;
            }
            set
            {
                _y2 = value;
            }
        }

        //Connection
        public bool IsConnectedTo(Segment seg)
        {
            if (X1 == seg.X1 && Y1 == seg.Y1)
                return true;
            if (X2 == seg.X1 && Y2 == seg.Y2)
                return true;
            if (X1 == seg.X2 && Y1 == seg.Y2)
                return true;
            if (X2 == seg.X2 && Y2 == seg.Y2)
                return true;
            return false;
        }

        //Access as points
        public Point Point1
        {
            get
            {
                return new Point(X1, Y1);
            }
        }
        public Point Point2
        {
            get
            {
                return new Point(X2, Y2);
            }
        }

        //Return the reverse of this segment
        public Segment Reversed
        {
            get
            {
                Segment s = new Segment();
                s.X1 = X2;
                s.Y1 = Y2;
                s.X2 = X1;
                s.Y2 = Y1;
                return s;
            }
        }

        //Offset the segment
        public void Offset(int x, int y)
        {
            _x1 += x;
            _y1 += y;
            _x2 += x;
            _y2 += y;
        }

        //equality
        public override bool Equals(object obj)
        {
            if (obj is Segment)
            {
                Segment s = (Segment)obj;
                if ((X1 == s.X1 && X2 == s.X2) || (X1 == s.X2 && X2 == s.X1))
                    if ((Y1 == s.Y1 && Y2 == s.Y2) || (Y1 == s.Y2 && Y2 == s.Y1))
                        return true;
                return false;
            }
            else
                return base.Equals(obj);
        }
        public static bool operator ==(Segment s1, Segment s2)
        {
            return s1.Equals(s2);
        }
        public static bool operator !=(Segment s1, Segment s2)
        {
            return !s1.Equals(s2);
        }
        public override int GetHashCode()
        {
            return _x1.GetHashCode() + _x2.GetHashCode() - _y1.GetHashCode() - _y2.GetHashCode();
        }

        //To string
        public override string ToString()
        {
            return X1 + "," + Y1 + " " + X2 + "," + Y2;
        }

        private int _x1;
        private int _y1;
        private int _x2;
        private int _y2;
    }
}
